﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DiaoPaiDaYin;
using DXApplicationTangche.DTO;
using DXApplicationTangche.DTO.坚山;
using DXApplicationTangche.service;
using DXApplicationTangche.service.redis_service;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.尺寸;
using DXApplicationTangche.UC.款式异常;
using DXApplicationTangche.UC.款式管理;
using DXApplicationTangche.UC.订单一览新.订单预览新.model;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.UC.订单一览新.辅料.辅料下单;
using DXApplicationTangche.UC.设计点;
using DXApplicationTangche.UC.货号管理;
using DXApplicationTangche.UC.货号管理.service;
using DXApplicationTangche.UC.辅料;
using DXApplicationTangche.UC.销售管理;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.UC.门店下单.form;
using DXApplicationTangche.UC.门店下单.service;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static mendian.Frm设计点;

namespace DXApplicationTangche.UC.门店下单.下单
{
    public partial class Form补充定制订信息 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        // 从页面返回的数据
        public OrderCommitDTO返回 orderCommitDTO;
        public List<备注DTOs> 备注DTOs;
        public Boolean 是否确认 = false;
        public String changeMaterialCode;

        //uc
        private XtraUserControlC设计点 UC设计点 { get; set; }
        //private XtraUserControl辅料选择 uC辅料 = new XtraUserControl辅料选择();
        //model
        private 订单预览新Model model;
        //dto
        public StyleAccessoryItemsDTO styleAccessoryItemsDTO;//辅料保存用dto
        public List<StyleAccessoryDTO> DTO辅料s = new List<StyleAccessoryDTO>();//辅料页面显示
        private List<面料选择> 面料选择s = new List<面料选择>();
        //变量
        private String stockInventoryNo { get; set; }//参考样衣成衣码
        //工序显示工艺设计
        private XtraUserControl工序显示设计 uc工序显示设计 = new XtraUserControl工序显示设计();

        public Form补充定制订信息(String refbarcode,String styleSizeCd,String saleConfirmId, String changeMaterialCode,String orderNumber)
        {
            InitializeComponent();
            this.changeMaterialCode = changeMaterialCode;
            // 工艺设计
            this.uc工序显示设计 = new XtraUserControl工序显示设计();
            this.uc工序显示设计.Dock = DockStyle.Fill;
            this.xtraTabPage工艺设计.Controls.Clear();
            this.xtraTabPage工艺设计.Controls.Add(this.uc工序显示设计);
            // 下单相关
            this.stockInventoryNo = refbarcode;
            this.textBox成衣条码.Text = refbarcode;
            try
            {
                this.model = new 订单预览新Model(refbarcode, Enum订单查询.按照水洗标查询订单);
                this.model.refBarcode = refbarcode;
            }
            catch (Exception)
            {
                MessageBox.Show("请扫描扫描成衣条码");
            }
            this.订单信息呈现();
            this.显示尺寸相关信息(styleSizeCd);
            this.buildSaleConfirmId(saleConfirmId);
            try { this.numericUpDown数量.Value = Convert.ToInt32(orderNumber); } catch { }
        }
        #region 控件
        
        private void 订单信息呈现()
        {
            //显示成衣码查询出内容，无参考意义的进行清空
            this.model.buildCustomerClear().build销售渠道Clear().buildOrderCommit().buildStyleCategoryDTOAndClear().buildClear刺绣();
            //门店定制下单销售渠道赋值
            this.model.build销售渠道("门店下单", "春衫定制");
            this.treeList尺寸.ExpandAll();
            //款式
            this.gridControl款式.DataSource = this.model.款式DTOs;
            //设计点
            this.model.设计点DTOs.Clear();
            this.UC设计点 = new XtraUserControlC设计点(this.model.OrderCommitDTO返回.styleNameCn);
            this.model.设计点DTOs = this.UC设计点.Dto设计点s;
            this.UC设计点.Dock = DockStyle.Fill;
            this.panel设计点.Controls.Clear();
            this.panel设计点.Controls.Add(this.UC设计点);
            // 辅料uc
            //this.uC辅料 =
            //    new XtraUserControl辅料选择(
            //        new 标识StyleAccessoryDTO(false, this.model.OrderCommitDTO返回.styleAccessoryDTOs),
            //        this.model.OrderCommitDTO返回.materialCode
            //        );
            //this.uC辅料.Dock = DockStyle.Fill;
            //this.panel辅料.Controls.Clear();
            //this.panel辅料.Controls.Add(this.uC辅料);
            //面料
            this.searchLookUpEdit面料.Text = "";
            //Frm面料选择.mianliaoid = this.model.面料DTOs[0].styleFabricId;
            Frm面料选择.mianliaocd = this.model.面料DTOs[0].materialCode;
            Frm面料选择.mianliao = this.model.面料DTOs[0].materialNameCn;
            this.textBox成分.Text = this.model.面料DTOs[0].composition;
            this.pictureBox面料图片.Image = this.model.面料DTOs[0].picture;
            //面料选择
            this.面料选择s = MTO订单Service.getMaterialOrderIndexAll();
            this.searchLookUpEdit面料.Properties.DataSource = this.面料选择s;
            //  如修改面料
            if (!this.changeMaterialCode.Equals(this.model.面料DTOs[0].materialCode))
            {
                this.面料在预下单时修改面料();
            }
            // 工艺设计
            this.uc工序显示设计.refreahData("",this.model.OrderCommitDTO返回.styleNameCn,true);
            //显示该款式的尺码组
            this.comboBox尺码组.Items.Clear();
            List<String> 尺码组 = new List<string>();
            尺码组 = FunctionHelper.JsonDeserialization<List<String>>(RestCall.httpGetMethod(URL.接口url + "/MTOOrder/customized/findSizeCdByStyleNameCn?styleNameCn=" + this.model.OrderCommitDTO返回.styleNameCn));
            foreach (String str in 尺码组)
            {
                this.comboBox尺码组.Items.Add(str);
            }
        }
        //  面料在预下单时修改面料
        private void 面料在预下单时修改面料()
        {
            foreach (面料选择 item in this.面料选择s)
            {
                if (this.changeMaterialCode.Equals(item.materialCode))
                {
                    //Frm面料选择.mianliaoid = item.materialId;
                    Frm面料选择.mianliao = item.materialNameCn;
                    Frm面料选择.mianliaocd = item.materialCode;
                    //给orderCommitDTO面料赋值
                    this.model.OrderCommitDTO返回.composition = item.materialComposition;
                    String MLpicurl = RestCall.httpGetMethod(URL.接口url + "/image/getImagePath?materialCode=" + Frm面料选择.mianliaocd);
                    if (String.IsNullOrWhiteSpace(MLpicurl) || MLpicurl.Contains("no"))
                    {
                        this.pictureBox面料图片.Image = DXApplicationTangche.Properties.Resources.SSHIRT;
                    }
                    else
                    {
                        this.pictureBox面料图片.LoadAsync(URL.图片url + URL.面料图片路径 + MLpicurl);
                    }
                    this.textBox成分.Clear();
                    this.textBox成分.Text = this.model.OrderCommitDTO返回.composition;
                    this.searchLookUpEdit面料.Text = Frm面料选择.mianliaocd + ":" + Frm面料选择.mianliao;
                    //  
                    //this.uC辅料 = new XtraUserControl辅料选择(true, Frm面料选择.mianliaocd);
                    //this.uC辅料.Dock = DockStyle.Fill;
                    //this.panel辅料.Controls.Clear();
                    //this.panel辅料.Controls.Add(this.uC辅料);
                    break;
                }
            }
        }
        // 选择尺寸
        private void 显示尺寸相关信息(String styleSizeCd)
        {
            try
            {
                this.comboBox尺码组.Text
                  = styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
            }
            catch { }
            this.model.OrderCommitDTO返回.styleSizeCd = styleSizeCd;
            try
            {
                this.model.尺寸 = 
                    FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(
                        RestCall.httpGetMethod(
                            URL.接口url + "/MTOOrder/customized/generateStyleMeasureItemsByStyleNameCn?SIZE_CD=" + this.model.OrderCommitDTO返回.styleSizeCd
                                        + "&styleNameCn=" + this.model.OrderCommitDTO返回.styleNameCn
                                        + "&categoryId=" + this.model.styleCategoryDTO.id
                                    )
                        );
                this.treeList尺寸.DataSource = this.model.尺寸.measureItemDTOS;
                this.treeList尺寸.Refresh();
            }
            catch
            {
                MessageBox.Show("无尺寸信息");
            }
        }
        // 选择尺寸
        private void comboBox尺码组_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.model.OrderCommitDTO返回.styleSizeCd = "IGS_GROUP_SIZE-" + this.comboBox尺码组.Text.Trim();
            try
            {
                String styleNameCn = this.model.OrderCommitDTO返回.styleNameCn;
                String SIZE_CD = this.model.OrderCommitDTO返回.styleSizeCd;
                StyleCategoryDTO styleCategoryDTO = this.model.styleCategoryDTO;

                this.model.尺寸 =
                    FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(
                        RestCall.httpGetMethod(
                            URL.接口url + "/MTOOrder/customized/generateStyleMeasureItemsByStyleNameCn?SIZE_CD=" + this.model.OrderCommitDTO返回.styleSizeCd
                                        + "&styleNameCn=" + this.model.OrderCommitDTO返回.styleNameCn
                                        + "&categoryId=" + this.model.styleCategoryDTO.id
                                    )
                        );
                this.treeList尺寸.DataSource = this.model.尺寸.measureItemDTOS;
                this.treeList尺寸.Refresh();
            }
            catch
            {
                MessageBox.Show("无尺寸信息");
            }
        }
        /// <summary>
        /// saleConfirmId
        /// </summary>
        private void buildSaleConfirmId(String saleConfirmId)
        {
            this.model.OrderCommitDTO返回.saleConfirmId = saleConfirmId;
        }

        private void numericUpDown数量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((NumericUpDown)sender).Text.Trim().IndexOf('.') > -1)
            {
                if (((NumericUpDown)sender).Text.Trim().Substring(((NumericUpDown)sender).Text.Trim().IndexOf('.') + 1).Length >= 0)//定义小数位
                {
                    if (!char.IsDigit(e.KeyChar))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
            }
        }
        private void treeList尺寸_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            try
            {
                if (this.treeList尺寸.FocusedColumn == this.treeListColumn加 || this.treeList尺寸.FocusedColumn == this.treeListColumn减)
                {
                    if (!FunctionHelper.IsFloat(e.Value.ToString()) || !FunctionHelper.Is半角(e.Value.ToString()))
                    {
                        e.ErrorText = "输入格式不正确！";
                        e.Valid = false;
                        return;
                    }
                }
            }
            catch { }
        }
        private void treeList尺寸_DoubleClick(object sender, EventArgs e)
        {
            new Form尺寸更改(
                this.model.尺寸,
                this.treeList尺寸.FocusedNode["id"].ToString())
                .ShowDialog();
        }
        private void treeList尺寸_CellValueChanged(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
        {
            try
            {
                this.model.尺寸.build算(this.treeList尺寸.FocusedNode["id"].ToString(), e.Column.FieldName, e.Value.ToString());
                this.treeList尺寸.DataSource = this.model.尺寸.measureItemDTOS;
                this.treeList尺寸.Refresh();
            }
            catch { }
        }

        private void richTextBox备注_TextChanged(object sender, EventArgs e)
        {
            this.model.OrderCommitDTO返回.remarks = this.richTextBox备注.Text.Trim();
        }

        private void barButtonItem保存_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (String.IsNullOrEmpty(this.comboBox尺码组.Text))
            {
                MessageBox.Show("选择款式尺寸");
                return;
            }
            DialogResult dialogResult = MessageBox.Show("确认保存吗？", "保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                if (!String.IsNullOrEmpty(this.UC设计点.vo刺绣内容.optionValue))
                {
                    if (String.IsNullOrWhiteSpace(this.UC设计点.vo刺绣位置.itemCd)
                        || String.IsNullOrWhiteSpace(this.UC设计点.vo刺绣大小.itemCd)
                        || String.IsNullOrWhiteSpace(this.UC设计点.vo刺绣字体.itemCd)
                        || String.IsNullOrWhiteSpace(this.UC设计点.vo刺绣颜色.itemCd))
                    {
                        MessageBox.Show("请将刺绣位置、大小、字体、颜色选择完全");
                        return;
                    }
                }
                if (!String.IsNullOrEmpty(this.UC设计点.vo刺绣内容.optionValue) 
                    && !String.IsNullOrWhiteSpace(this.UC设计点.vo刺绣位置.itemCd)
                    && !String.IsNullOrWhiteSpace(this.UC设计点.vo刺绣大小.itemCd)
                    && !String.IsNullOrWhiteSpace(this.UC设计点.vo刺绣字体.itemCd)
                    && !String.IsNullOrWhiteSpace(this.UC设计点.vo刺绣颜色.itemCd))
                {
                    this.model.设计点DTOs.Add(new DTODesignPoints(this.UC设计点.vo刺绣位置));
                    this.model.设计点DTOs.Add(new DTODesignPoints(this.UC设计点.vo刺绣大小));
                    this.model.设计点DTOs.Add(new DTODesignPoints(this.UC设计点.vo刺绣字体));
                    this.model.设计点DTOs.Add(new DTODesignPoints(this.UC设计点.vo刺绣颜色));
                    this.model.设计点DTOs.Add(new DTODesignPoints(this.UC设计点.vo刺绣内容));
                }
                //保存orderCommit相关属性
                this.model.buildOrderCommitDTOSample(this.stockInventoryNo).buildModelOrderCommitDTO();
                this.model.OrderCommitDTO返回.orderNumber = Convert.ToDouble(this.numericUpDown数量.Value.ToString());
                //this.model.OrderCommitDTO返回.styleAccessoryDTOs = this.uC辅料.build().styleAccessoryDTOs;
                //保存单个订单
                this.orderCommitDTO = this.model.OrderCommitDTO返回;
                this.备注DTOs = this.model.备注DTOs;
                this.是否确认 = true;
                MessageBox.Show("保存成功");
                this.Close();
            }
        }

        #region 选择面料
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit面料_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit面料View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit面料.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                //Frm面料选择.mianliaoid = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "materialId").ToString();
                Frm面料选择.mianliao = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "materialNameCn").ToString();
                Frm面料选择.mianliaocd = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "materialCode").ToString();
                //给orderCommitDTO面料赋值
                this.model.OrderCommitDTO返回.composition = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "materialComposition").ToString();
                String MLpicurl = RestCall.httpGetMethod(URL.接口url + "/image/getImagePath?materialCode=" + Frm面料选择.mianliaocd);
                if (String.IsNullOrWhiteSpace(MLpicurl)|| MLpicurl.Contains("no"))
                {
                    this.pictureBox面料图片.Image = DXApplicationTangche.Properties.Resources.SSHIRT;
                }
                else
                {
                    this.pictureBox面料图片.LoadAsync(URL.图片url + URL.面料图片路径 + MLpicurl);
                }
                this.textBox成分.Clear();
                this.textBox成分.Text = this.model.OrderCommitDTO返回.composition;
            }
            //this.uC辅料 = new XtraUserControl辅料选择(true, Frm面料选择.mianliaocd);
            //this.uC辅料.Dock = DockStyle.Fill;
            //this.panel辅料.Controls.Clear();
            //this.panel辅料.Controls.Add(this.uC辅料);
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit面料.ToolTip = null;
            searchLookUpEdit面料.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit面料_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = Frm面料选择.mianliaocd + ":" + Frm面料选择.mianliao;
        }
        #endregion
        
        #region 备注图片
        private void pictureBox备注图片1_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (备注DTOs rp in this.model.备注DTOs)
                {
                    if ("1".Equals(rp.id))
                    {
                        this.model.备注DTOs.Remove(rp);
                        break;
                    }
                }
                this.model.备注DTOs.Add(new 备注DTOs("1", Image.FromFile(ofd.FileName),ofd.FileName));
                this.pictureBox备注图片1.Image = Image.FromFile(ofd.FileName);
            }
        }

        private void pictureBox备注图片2_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (备注DTOs rp in this.model.备注DTOs)
                {
                    if ("2".Equals(rp.id))
                    {
                        this.model.备注DTOs.Remove(rp);
                        break;
                    }
                }
                this.model.备注DTOs.Add(new 备注DTOs("2", Image.FromFile(ofd.FileName), ofd.FileName));
                this.pictureBox备注图片2.Image = Image.FromFile(ofd.FileName);
            }
        }

        private void pictureBox备注图片3_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (备注DTOs rp in this.model.备注DTOs)
                {
                    if ("3".Equals(rp.id))
                    {
                        this.model.备注DTOs.Remove(rp);
                        break;
                    }
                }
                this.model.备注DTOs.Add(new 备注DTOs("3", Image.FromFile(ofd.FileName),ofd.FileName));
                this.pictureBox备注图片3.Image = Image.FromFile(ofd.FileName);
            }
        }

        
        private void 删除备注图片1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (备注DTOs rp in this.model.备注DTOs)
            {
                if (1.Equals(rp.id))
                {
                    this.model.备注DTOs.Remove(rp);
                    this.pictureBox备注图片1.Image = null;
                    break;
                }
            }
        }

        private void 删除备注图片2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (备注DTOs rp in this.model.备注DTOs)
            {
                if (2.Equals(rp.id))
                {
                    this.model.备注DTOs.Remove(rp);
                    this.pictureBox备注图片2.Image = null;
                    break;
                }
            }
        }

        private void 删除备注图片3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (备注DTOs rp in this.model.备注DTOs)
            {
                if (3.Equals(rp.id))
                {
                    this.model.备注DTOs.Remove(rp);
                    this.pictureBox备注图片3.Image = null;
                    break;
                }
            }
        }
        #endregion
        //选择返单号
        private void gridView历史订单_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            this.model.OrderCommitDTO返回.refOrderId = FunctionHelper.GetgridViewValue(
                                    this.gridView历史订单,
                                    this.gridView历史订单.FocusedRowHandle,
                                    "orderId"
                                    );
            this.model.OrderCommitDTO返回.refOrderNo = FunctionHelper.GetgridViewValue(
                                    this.gridView历史订单,
                                    this.gridView历史订单.FocusedRowHandle,
                                    "orderNo"
                                    );
            this.textBox返单号.Text = this.model.OrderCommitDTO返回.refOrderNo;
        }
        #endregion

    }
}
